package com.sduept.nwld.dataserver.controller.assessment;

import com.sduept.bigdata.fault.model.CTSaturation;
import com.sduept.bigdata.fault.special.maintenance.manager.CtsaturatedQueryManager;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.cim.model.wires.ACLineSegment;
import com.sduept.cim.query.service.ACLineSegmentQueryService;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.utils.DateUtil;
import com.sduept.utils.PingYinUtil;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.*;

/**
 * CT运行风险评估
 */
@Named("cTSaturationController")
@ViewScoped
public class CTSaturationController extends AbstractController {

	private static final long serialVersionUID = 1L;

	@Autowired
	private CtsaturatedQueryManager ct;
	@Autowired
	private CimManager cim;
	@Autowired
	private ACLineSegmentQueryService aCLineSegmentQueryService;

	private List<CTSaturation> ctModel = new ArrayList<>();
	private String stationName;
	private String lineName;
	private Date faultTimest;
	private Date faultTimeet;
	private boolean saturate; // 1:ture;0:false
	/**
	 * 谐波比
	 */
	private String seventhPowerHarmonicProportion = "0.12";
	/**
	 * 面积比
	 */
	private String areaProportion = "1.5";
	public List<ACLineSegment> acs = new ArrayList<>();
	public Map<String, List<String>> acline = new HashMap<>();

	@PostConstruct
	private void init() {
		saturate = true;
		Calendar calendar = Calendar.getInstance();
		faultTimeet = calendar.getTime();
		calendar.set(calendar.get(Calendar.YEAR), 0, 1);
		faultTimest = calendar.getTime();
		ctModel = ct.getSaturationByCondition(stationName, lineName, faultTimest, faultTimeet, saturate, seventhPowerHarmonicProportion, areaProportion);
		acs.addAll(aCLineSegmentQueryService.listAcLineSegments(500, null));
		for (ACLineSegment r : acs) {
			String key = PingYinUtil.getPingYinHeader(r.getName());
			List<String> ss = acline.get(key);
			if (ss == null) {
				ss = new ArrayList<>();
				acline.put(key, ss);
			}
			ss.add(r.getName());
		}
	}

	/*
	 * //组合条件查询 public void selectByCondition(){ ctModel.clear();
	 * ctModel=ct.getSaturationByCondition(saturate); }
	 */

	// 组合条件查询
	public void selectByCondition() {
		ctModel.clear();
//    	 @param {"stationName":stationName,"lineName":lineName,"faultTimest":faultTimestst,"faultTimeet":faultTimestet,"saturate":saturate}
		String stationId = "";
		if (stationName != null && !(stationName.equals(""))) {
			stationId = ct.getStationIdByName(stationName);
		}
		faultTimeet = DateUtil.addDays(faultTimeet, 1);
		faultTimeet = DateUtil.addSeconds(faultTimeet, -1);
		ctModel = ct.getSaturationByCondition(stationId, lineName, faultTimest, faultTimeet, saturate, seventhPowerHarmonicProportion, areaProportion);
	}

	// 查看全部
	public void selectAll() {
		ctModel.clear();
		ctModel = ct.getCTSaturation();
	}

	// 根据站名自动过滤线路
	public void autoFilterAcline(SelectEvent event) {
		acs.clear();
		acline.clear();
		String sname = event.getObject().toString();
		String sId = cim.getSingelStationIdByName(sname);
		acs = cim.getAcLineSegmentsBySubstation(sId);
		for (ACLineSegment r : acs) {
			String key = PingYinUtil.getPingYinHeader(r.getName());
			List<String> ss = acline.get(key);
			if (ss == null) {
				ss = new ArrayList<>();
				acline.put(key, ss);
			}
			ss.add(r.getName());
		}
	}

	public List<String> completeAcLine(String query) {
		List<String> results = new ArrayList<String>();
		for (String s : acline.keySet()) {
			if (s.startsWith(query))
				results.addAll(acline.get(s));
		}
		return results;
	}

	public List<CTSaturation> getCtModel() {
		return ctModel;
	}

	public void setCtModel(List<CTSaturation> ctModel) {
		this.ctModel = ctModel;
	}

	public String getStationName() {
		return stationName;
	}

	public void setStationName(String stationName) {
		this.stationName = stationName;
	}

	public String getLineName() {
		return lineName;
	}

	public void setLineName(String lineName) {
		this.lineName = lineName;
	}

	public Date getfaultTimest() {
		return faultTimest;
	}

	public void setfaultTimest(Date faultTimest) {
		this.faultTimest = faultTimest;
	}

	public Boolean getSaturate() {
		return saturate;
	}

	public void setSaturate(Boolean saturate) {
		this.saturate = saturate;
	}

	public Date getFaultTimeet() {
		return faultTimeet;
	}

	public void setFaultTimeet(Date faultTimeet) {
		this.faultTimeet = faultTimeet;
	}

	public String getSeventhPowerHarmonicProportion() {
		return seventhPowerHarmonicProportion;
	}

	public void setSeventhPowerHarmonicProportion(String seventhPowerHarmonicProportion) {
		this.seventhPowerHarmonicProportion = seventhPowerHarmonicProportion;
	}

	public String getAreaProportion() {
		return areaProportion;
	}

	public void setAreaProportion(String areaProportion) {
		this.areaProportion = areaProportion;
	}

}